<?php
declare(strict_types=1);

namespace ImiApp\ApiServer\Backend\Controller;

use Imi\Server\Http\Route\Annotation\Action;
use Imi\Server\Http\Route\Annotation\Controller;
use Imi\Server\Http\Route\Annotation\Route;
use Phpben\Imi\Validate\Annotation\Validate;
use Psr\Http\Message\ResponseInterface;
use Imi\Aop\Annotation\Inject;
use Phpben\Imi\Auth\Annotation\Auth;

use ImiApp\ApiServer\Backend\Controller\CommonController as CommonController;

/**
 * @Auth(name="backend")
 * @Controller("/goodsbrand/")
 */
class GoodsbrandController extends CommonController
{
    /**
     * @Inject("GoodsbrandService")
     */
    protected $service;

    /**
     * @Action
     * @Route("read")
     * @return ResponseInterface
     */
    public function read(): ResponseInterface
    {
        $ids = $this->request->get('ids',null);
        if($ids!=null){
            $list =\Imi\Db\Db::name('goodsbrand')->whereIn('id',explode(',',$ids))->select()->getArray();
            return $this->response->success(null, $list);
        }

        $goodscate_ids = $this->request->get('goodscate_ids',null);
        if($goodscate_ids!=null){
            //获取下级id
            
            $goodscate_ids2 =\Imi\Db\Db::name('goodscate')->whereIn('pid',explode(',',$goodscate_ids))->orderRaw('weigh desc')->column('id');

            $list =\Imi\Db\Db::name('goodsbrand')->whereIn('goodscate_id',$goodscate_ids2)->orderRaw('goodscate_id desc,weigh desc')->select()->getArray();
            if($list){
                foreach ($list as $key => &$val) {
                    $val['goodscate_pid'] = \Imi\Db\Db::name('goodscate')->where('id','=',$val['goodscate_id'])->value('pid');

                }
            }
            return $this->response->success(null, $list);
        }

        return $this->response->success(null, $this->service->read() ?: []);
    }





    /**
     * @Action
     * @Route(url="create",method="POST")
     * @Validate
     * @param $data
     * @return ResponseInterface
     */
    public function create($data): ResponseInterface
    {
        return $this->service->create($data) ? $this->response->success('创建成功') : $this->response->error($this->service->getError());
    }

    /**
     * @Action
     * @Route(url="update",method="POST")
     * @Validate
     * @param $data
     * @return ResponseInterface
     */
    public function update($data): ResponseInterface
    {
        return $this->service->update($data) ? $this->response->success('更新成功') : $this->response->error($this->service->getError());
    }

    /**
     * @Action
     * @Route(url="delete",method="POST")
     * @return ResponseInterface
     */
    public function delete(): ResponseInterface
    {
        return $this->service->delete() ? $this->response->success('删除成功') : $this->response->error($this->service->getError());
    }

    /**
     * @Action
     * @Route(url="operate",method="POST")
     * @return ResponseInterface
    */
    public function operate(): ResponseInterface
    {
        return $this->service->operate() ? $this->response->success('操作成功') : $this->response->error($this->service->getError());
    }

    /**
     * @Action
     * @Route(url="sort",method="POST")
     * @Validate
     * @param $data
     * @return ResponseInterface
    */
    public function sort($data): ResponseInterface
    {
        return $this->service->draggable($data) ? $this->response->success('排序成功') : $this->response->error($this->service->getError());
    }    
}